package com.supermarket.manage.common.utils;

import com.supermarket.manage.common.dto.ucenter.response.MenuNode;
import com.supermarket.manage.common.pojo.Permission;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-04-23 10:54
 **/
public class MenuUtil {
    /**
     * 所有权限
     */
    public static String all = "/";

    /**
     * 用户
     */
    public static String getUser = "/user/get";
    public static String manageUser = "/user/manage";
    /**
     * 商品
     */
    public static String manageProduct = "/product/manage";
    /**
     * 销售
     */
    public static String getReturnInstock = "/instock/return/get";
    public static String manageReturnInstock = "/instock/return/manage";
    public static String getSaleOutstock = "/outstock/sale/get";
    public static String manageSaleOutstock = "/outstock/sale/manage";
    /**
     * 采购
     */
    public static String manageSupplier = "/supplier/manage";
    public static String getPurchaseInstock = "/instock/purchase/get";
    public static String managePurchaseInstock = "/instock/purchase/manage";
    public static String getBackOutstock = "/outstock/back/get";
    public static String manageBackOutstock = "/outstock/back/manage";
    /**
     * 库存
     */
    public static String getStocking = "/stocking/get";
    public static String manageStocking = "/stocking/manage";
    /**
     * 会员
     */
    public static String manageMember = "/member/manage";

    public static List<MenuNode> getMenuByPermissions(List<Permission> permissions) {
        List<String> paths = permissions.stream().map(Permission::getUrl).collect(Collectors.toList());
        if (paths.contains(all)) {
            return getAllMenu();
        }
        List<MenuNode> menuList = new ArrayList<>();
        MenuNode report = new MenuNode("145", "/salesman/firstNum", "首页");
        menuList.add(report);

        MenuNode ucenter = new MenuNode("125", null, "用户");
        List<MenuNode.ChildMenuNode> ucenterChildren = ucenter.getChildren();
        ucenterChildren.add(MenuNode.ChildMenuNode.USER_CENTER);
        if (paths.contains(getUser)) {
            ucenterChildren.add(MenuNode.ChildMenuNode.USER_MANAGE);
        }
        if (paths.contains(manageUser)) {
            ucenterChildren.add(MenuNode.ChildMenuNode.ROLE_MANAGE);
        }
        menuList.add(ucenter);

        if (paths.contains(manageProduct)) {
            MenuNode product = new MenuNode("103", null, "商品");
            List<MenuNode.ChildMenuNode> productChildren = product.getChildren();
            productChildren.add(MenuNode.ChildMenuNode.PRODUCT);
            productChildren.add(MenuNode.ChildMenuNode.CATEGORY);
            menuList.add(product);
        }

        MenuNode sale = new MenuNode("102", null, "销售");

        List<MenuNode.ChildMenuNode> saleChildren = sale.getChildren();
        if (paths.contains(manageSaleOutstock)) {
            saleChildren.add(MenuNode.ChildMenuNode.CREATE_SALE_OUTSTOCK);
        }
        if (paths.contains(manageReturnInstock)) {
            saleChildren.add(MenuNode.ChildMenuNode.CREATE_SALE_INSTOCK);
        }
        if (paths.contains(getReturnInstock)) {
            saleChildren.add(MenuNode.ChildMenuNode.SALE_OUTSTOCK_LIST);
        }
        if (paths.contains(getReturnInstock)) {
            saleChildren.add(MenuNode.ChildMenuNode.SALE_INSTOCK_LIST);
        }
        if (saleChildren.size() > 0) {
            menuList.add(sale);
        }

        MenuNode purchase = new MenuNode("100", null, "采购");
        List<MenuNode.ChildMenuNode> purchaseChildren = purchase.getChildren();
        if (paths.contains(manageSupplier)) {
            purchaseChildren.add(MenuNode.ChildMenuNode.SUPPLIER_MANAGE);
        }
        if (paths.contains(managePurchaseInstock)) {
            purchaseChildren.add(MenuNode.ChildMenuNode.CREATE_PURCHASE_INSTOCK);
        }
        if (paths.contains(manageBackOutstock)) {
            purchaseChildren.add(MenuNode.ChildMenuNode.CREATE_PURCHASE_OUTSTOCK);
        }
        if (paths.contains(getPurchaseInstock)) {
            purchaseChildren.add(MenuNode.ChildMenuNode.PURCHASE_INSTOCK_LIST);
        }
        if (paths.contains(getBackOutstock)) {
            purchaseChildren.add(MenuNode.ChildMenuNode.PURCHASE_OUTSTOCK_LIST);
        }
        if(purchaseChildren.size()>0){
            menuList.add(purchase);
        }

        MenuNode stock = new MenuNode("99", null, "库存");
        List<MenuNode.ChildMenuNode> stockChildren = stock.getChildren();
        if(paths.contains(manageStocking)){
            stockChildren.add(MenuNode.ChildMenuNode.CREAT_STOCKING);
        }
        if (paths.contains(getStocking)) {
            stockChildren.add(MenuNode.ChildMenuNode.STOCKING_LIST);
        }
        if(stockChildren.size()>0){
            menuList.add(stock);
        }
        if(paths.contains(manageMember)){
            MenuNode member = new MenuNode("140", "/salesman/vipMessage", "会员");
            menuList.add(member);
        }

        MenuNode service = new MenuNode("666", "/salesman/smallTalk", "客服");
        menuList.add(service);

        return menuList;
    }

    public static List<MenuNode> getAllMenu() {
        List<MenuNode> menuList = new ArrayList<>();

        MenuNode report = new MenuNode("145", "/salesman/firstNum", "首页");
        menuList.add(report);

        MenuNode ucenter = new MenuNode("125", null, "用户");
        List<MenuNode.ChildMenuNode> ucenterChildren = ucenter.getChildren();
        ucenterChildren.add(MenuNode.ChildMenuNode.USER_CENTER);
        ucenterChildren.add(MenuNode.ChildMenuNode.USER_MANAGE);
        ucenterChildren.add(MenuNode.ChildMenuNode.ROLE_MANAGE);
        menuList.add(ucenter);

        MenuNode product = new MenuNode("103", null, "商品");
        List<MenuNode.ChildMenuNode> productChildren = product.getChildren();
        productChildren.add(MenuNode.ChildMenuNode.PRODUCT);
        productChildren.add(MenuNode.ChildMenuNode.CATEGORY);
        menuList.add(product);

        MenuNode sale = new MenuNode("102", null, "销售");
        List<MenuNode.ChildMenuNode> saleChildren = sale.getChildren();
        saleChildren.add(MenuNode.ChildMenuNode.CREATE_SALE_OUTSTOCK);
        saleChildren.add(MenuNode.ChildMenuNode.CREATE_SALE_INSTOCK);
        saleChildren.add(MenuNode.ChildMenuNode.SALE_OUTSTOCK_LIST);
        saleChildren.add(MenuNode.ChildMenuNode.SALE_INSTOCK_LIST);
        menuList.add(sale);

        MenuNode purchase = new MenuNode("100", null, "采购");
        List<MenuNode.ChildMenuNode> purchaseChildren = purchase.getChildren();
        purchaseChildren.add(MenuNode.ChildMenuNode.SUPPLIER_MANAGE);
        purchaseChildren.add(MenuNode.ChildMenuNode.CREATE_PURCHASE_INSTOCK);
        purchaseChildren.add(MenuNode.ChildMenuNode.CREATE_PURCHASE_OUTSTOCK);
        purchaseChildren.add(MenuNode.ChildMenuNode.PURCHASE_INSTOCK_LIST);
        purchaseChildren.add(MenuNode.ChildMenuNode.PURCHASE_OUTSTOCK_LIST);
        menuList.add(purchase);

        MenuNode stock = new MenuNode("99", null, "库存");
        List<MenuNode.ChildMenuNode> stockChildren = stock.getChildren();
        stockChildren.add(MenuNode.ChildMenuNode.CREAT_STOCKING);
        stockChildren.add(MenuNode.ChildMenuNode.STOCKING_LIST);
        menuList.add(stock);

        MenuNode member = new MenuNode("140", "/salesman/vipMessage", "会员");
        menuList.add(member);
        MenuNode service = new MenuNode("666", "/salesman/smallTalk", "客服");
        menuList.add(service);
        return menuList;
    }
}
